﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text.RegularExpressions;
namespace FTchina.Api
{
    /// <summary>
    ///NewsList 的摘要说明
    /// </summary>
    public class NewsList
    {

        #region 各项准备工作
        //递归形成路径层次
        static string ids = string.Empty;

        private static string MLSqlPart(string id)
        {
            ids = "";
            string SqlPart = string.Empty;
            MLcc(id);
            if (ids.Length > 0)
            {
                ids = ids.Substring(0, ids.Length - 1);
                string[] temp = ids.Split(',');
                for (int i = 0; i < temp.Length; i++)
                {
                    SqlPart += " lanmuid='" + temp[i] + "' or ";
                }
            }
            return SqlPart;
        }
        private static void MLcc(string lmid)
        {

            DataView dv = new DataView(shujuku.GetDataTable("select * from lanmu where parentid=" + lmid + " order by id asc", "DataView" + lmid));
            foreach (DataRowView drv in dv)
            {
                if (drv["id"].ToString() != "")
                {
                    ids += drv["id"] + ",";
                    MLcc(drv["id"].ToString());
                }
            }


        }


        private static string cuttitle(string ftitle, int length)
        {
            if (ftitle.Length > length)
            {
                ftitle = ftitle.Substring(0, length) + "…";
            }
            return ftitle;
        }
        //判断时间差,是否显示new图标
        private static string chkchownew(string time, int days)
        {
            DateTime dt1 = Convert.ToDateTime(time);
            DateTime dt2 = DateTime.Now;
            TimeSpan dt3 = dt2.Subtract(dt1);
            if (dt3.Days < days) return "<img src=" + basic.GetSiteUrl() + "images/icon/new.gif />";
            else return "";

        }
        #endregion

        /// <summary>
        /// 获取新闻列表
        /// </summary>
        /// <param name="id">栏目id，0代表所有的栏目</param>
        /// <param name="list">显示list条新闻</param>
        /// <param name="timetype">
        /// 0:自动处理
        /// 1：yyyy年MM月dd日
        /// 2：MM月dd日
        /// 3：MM月dd日H时
        /// 4：yyyy年MM月dd日H时
        /// </param>
        /// <param name="words">标题显示words个字符</param>
        /// <param name="open">"0"或者不填写为"_self",其他的请行填写，如"_blank"</param>
        /// <param name="news">0代表不显示，1代表1天内的显示news图标，2代表2天内的显示news图标</param>
        /// <param name="lmname">0代表不显示栏目名称，1代表显示</param>
        /// <param name="hit">0代表不显示点击数，1代表显示</param>
        /// <param name="hot">0代表不按照点击量排序</param>
        /// <param name="dtj">0代表不显示【顶、荐、图】等字样，1代表显示</param>
        /// <param name="date">0代表不显示日期，1代表显示</param>
        /// <returns></returns>
        public static string GetList(int id, int list, int timetype, int words, string open, int news, int lmname, int hit, int hot, int dtj, int date)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("GetList" + id.ToString() + list.ToString() + timetype.ToString() + words.ToString() + open + news.ToString() + lmname.ToString() + hit.ToString() + hot.ToString() + dtj.ToString() + date.ToString()))
            {
                return FTchina.Caching.FTchinaCache.GetCache("GetList" + id.ToString() + list.ToString() + timetype.ToString() + words.ToString() + open + news.ToString() + lmname.ToString() + hit.ToString() + hot.ToString() + dtj.ToString() + date.ToString());
            }
            else
            {
                DataTable dt;
                string tempwords = string.Empty;
                if (open.Length == 0) open = "_self ";
                string TBhtml = string.Empty;
                string newsflag = string.Empty;
                string lmflag = string.Empty;
                string hitflag = string.Empty;
                string imgflag = string.Empty;
                string topflag = string.Empty;
                string tjflag = string.Empty;
                string hotflag = string.Empty;
                string urlflag = string.Empty;
                string dtjflag = string.Empty;
                string dataflag = string.Empty;
                if (hot == 1) hotflag = " hit desc,";

                string GetListSql = "select top " + list + " * from content where isdel=0 and ispass=1 and ( " + MLSqlPart(id.ToString()) + " lanmuid='" + id + "') order by " + hotflag + " istop desc,tj desc,id desc";
                dt = shujuku.GetDataTable(GetListSql, "newslist" + id + list);

                if (dt.Rows.Count > 0)
                {//有新闻


                    int listflag = Convert.ToInt32(list);
                    if (dt.Rows.Count < listflag) listflag = dt.Rows.Count;
                    TBhtml = " <ul class='nl'>";
                    for (int i = 0; i < listflag; i++)
                    {
                        string colorflag = string.Empty;

                        if (dt.Rows[i]["url"].ToString() != "")
                        {
                            urlflag = basic.GetSiteUrl() + "site/r" + dt.Rows[i]["id"].ToString() + ".aspx";
                        }
                        else
                        {
                            urlflag = Api.htmlpath.GetFullPath(dt.Rows[i]["id"].ToString(), "1", dt.Rows[i]["data"].ToString());

                        }
                        imgflag = (dt.Rows[i]["isimg"].ToString() == "1") ? "[图]" : "";
                        topflag = (dt.Rows[i]["istop"].ToString() == "1") ? "[顶]" : "";
                        tjflag = (dt.Rows[i]["tj"].ToString() == "1") ? "[荐]" : "";
                        dataflag = (date == 1) ? FTchina.DataTimeFormat.FormatTime(dt.Rows[i]["data"].ToString(), timetype.ToString()) : "";
                        if (hit == 1) hitflag = "<span style='color:Gray'>&nbsp;&nbsp;&nbsp;&nbsp;(" + dt.Rows[i]["hit"].ToString() + ")</span>";
                        if (news != 0) newsflag = chkchownew(dt.Rows[i]["data"].ToString(), news);
                        if (lmname == 1) lmflag = "[" + dt.Rows[i]["lanmuname"].ToString() + "]";
                        if (dtj == 1) dtjflag = topflag + tjflag + imgflag;
                        if (dt.Rows[i]["tcolor"].ToString().Trim() != "Transparent")
                        {
                            colorflag = "style=color:" + dt.Rows[i]["tcolor"].ToString() + "";
                        }

                        TBhtml += "<li>" + lmflag + dtjflag + " <a target=" + open + colorflag + " href=" + urlflag + " >" + cuttitle(dt.Rows[i]["title"].ToString(), words) + "</a>" +
                             hitflag + newsflag + "\r\n" +
                          dataflag + "</li>";
                    }
                    TBhtml += "</ul>";

                }
                else
                {//无新闻
                    TBhtml = "<li>此分类暂无新闻，敬请期待</li>";
                }

                FTchina.Caching.FTchinaCache.AddCache("GetList" + id.ToString() + list.ToString() + timetype.ToString() + words.ToString() + open + news.ToString() + lmname.ToString() + hit.ToString() + hot.ToString() + dtj.ToString() + date.ToString(), TBhtml);

                return TBhtml;
            }
        }
        /// <summary>
        /// 获取上下条
        /// </summary>
        /// <param name="id">文章id</param>
        /// <param name="up">获取方向,up为空取下一条，up非空取上一条</param>
        /// <returns></returns>
        public static string Getlink(string id, string up)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("Getlink" + up + id))
            {
                return FTchina.Caching.FTchinaCache.GetCache("Getlink" + up + id);
            }
            else
            {
                string tempwords;
                string syt = string.Empty;
                string xyt = string.Empty;
                string lanmuid = shujuku.GetValueByKey("lanmuid", "content", "id=" + id);
                DataTable dt;

                if (up.Length > 0)
                {
                    string sytsql = "select top 1 title,id,url,data from content where ( ispass=1 and isdel=0 and  lanmuid='" + lanmuid + "' and id<" + id + ") order by id desc";
                    if (shujuku.testread(sytsql))
                    {
                        dt = shujuku.GetDataTable(sytsql);
                        if (dt.Rows[0]["url"].ToString() != "")
                        {
                            syt = "<a href='" + basic.GetSiteUrl() + "site/r" + dt.Rows[0]["id"].ToString() + ".aspx' traget='_blank'>" + dt.Rows[0]["title"].ToString() + "</a>";

                        }
                        else
                        {
                            string urlflag = Api.htmlpath.GetFullPath(dt.Rows[0]["id"].ToString(), "1", dt.Rows[0]["data"].ToString());


                            syt = "<a href=" + urlflag + " >" + dt.Rows[0]["title"].ToString() + "</a>";
                        }
                    }
                    else
                    {
                        syt = "已经没有了";
                    }
                    tempwords = syt;
                }
                else
                {
                    string xytsql = "select top 1 title,id,url,data from content where (ispass=1 and isdel=0 and  lanmuid='" + lanmuid + "' and id>" + id + ")";
                    if (shujuku.testread(xytsql))
                    {
                        dt = shujuku.GetDataTable(xytsql);
                        if (dt.Rows[0]["url"].ToString() != "")
                        {
                            xyt = "<a href='" + basic.GetSiteUrl() + "site/r" + dt.Rows[0]["id"].ToString() + ".aspx' traget='_blank'>" + dt.Rows[0]["title"].ToString() + "</a>";

                        }
                        else
                        {
                            string urlflag = Api.htmlpath.GetFullPath(dt.Rows[0]["id"].ToString(), "1", dt.Rows[0]["data"].ToString());

                            xyt = "<a href=" + urlflag + " >" + dt.Rows[0]["title"].ToString() + "</a>";
                        }
                    }
                    else
                    {
                        xyt = "已经没有了";
                    }
                    tempwords = xyt;
                }
                FTchina.Caching.FTchinaCache.AddCache("Getlink" + up + id, tempwords);

                return tempwords;
            }
        }


        /// <summary>
        /// 获取相关新闻
        /// </summary>
        /// <param name="artid"></param>
        /// <returns></returns>
        public static string GetReleaseList(string artid, int limit, int words)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("GetReleaseList" + limit + words))
            {
                return FTchina.Caching.FTchinaCache.GetCache("GetReleaseList" + limit + words);
            }
            else
            {

                string tempwords = string.Empty;
                string keys = shujuku.GetValueByKey("keys", "content", "id=" + artid, "artkeys");
                DataTable dt = shujuku.GetDataTable("select  top " + limit + " * from content where keys like'%" + keys + "%' and id<>" + artid, "GetReleaseList");
                tempwords += "<ul class='nl'>";

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string urlflag = Api.htmlpath.GetFullPath(dt.Rows[i]["id"].ToString(), "1", dt.Rows[i]["data"].ToString());

                    tempwords += "<li><a href=" + urlflag + " >" + basic.GetSubString(dt.Rows[i]["title"].ToString(), words, "…") + "</a>&nbsp; <span class=\"gray\">(" + FTchina.DataTimeFormat.FormatTime((DateTime)dt.Rows[i]["data"]) + ")</span></li>";
                }
                tempwords += "</ul>";
                FTchina.Caching.FTchinaCache.AddCache("GetReleaseList" + limit + words, tempwords);

                return tempwords;
            }
        }


        /// <summary>
        /// 获取多少条随机记录
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string GetRandomList(int count, int words)
        {
            string tempwords = string.Empty;
            string redirect = string.Empty;
            DataTable dt = shujuku.GetDataTable("select  top " + count + " id,title,url,data from content order by newid()");
            tempwords += "<ul class='nl'>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["url"].ToString() != "")
                {
                    tempwords += "<li><a href= " + basic.GetSiteMainWorkUrl() + "r" + dt.Rows[i]["id"] + ".aspx >" + basic.GetSubString(dt.Rows[i]["title"].ToString(), words, "…") + "</a></li>";
                }
                else
                {
                    string urlflag = Api.htmlpath.GetFullPath(dt.Rows[i]["id"].ToString(), "1", dt.Rows[i]["data"].ToString());
                    tempwords += "<li><a href=" + urlflag + ">" + basic.GetSubString(dt.Rows[i]["title"].ToString(), words, "…") + "</a></li>";
                }
            }
            tempwords += "</ul>";

            return tempwords;

        }
        #region 图片新闻

        /// <summary>
        /// 获取指定栏目flash图片新闻
        /// </summary>
        /// <param name="width">输出宽度</param>
        /// <param name="height">输出高度</param>
        /// <param name="list">条目数量</param>
        /// <param name="lanmuid">指定id</param>
        /// <returns></returns>
        public static string GetFalshlist(int width, int height, int list, int lanmuid)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("flash" + list))
            {
                return FTchina.Caching.FTchinaCache.GetCache("flash" + list);
            }
            else
            {

                string flag = (lanmuid == 0) ? " 1=1 " : "lanmuid=" + lanmuid;

                int ImgNewsCount = shujuku.GetDataTable("SELECT  * FROM content where isdel=0 and ispass=1 and isimg=1 and " + flag + "").Rows.Count;
                int listflag = (Convert.ToInt32(list) > ImgNewsCount) ? ImgNewsCount : Convert.ToInt32(list);

                if (listflag == 0) return "暂无";
                else
                {
                    string comm = "SELECT top " + listflag + " title,url,data,id,img,content FROM content where isdel=0 and ispass=1 and isimg=1 and " + flag + " ORDER BY id DESC ";

                    DataTable dt = shujuku.GetDataTable(comm);


                    string imgurl = null;
                    string imgtext = null;
                    string imglink = null;
                    string imgurl1 = null;
                    string imgtext1 = null;
                    string imglink1 = null;
                    string Words = null;

                    for (int i = 0; i < listflag; i++)
                    {

                        string imgurltemp = dt.Rows[i]["img"].ToString();
                        if (imgurltemp.Length == 0)
                        {
                            Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^\']*)')", RegexOptions.IgnoreCase);
                            MatchCollection m = reg.Matches(dt.Rows[i]["content"].ToString());
                            imgurltemp = m[0].Groups[1].Value;
                        }
                        imgurl1 = imgurl1 + "|" + imgurltemp;

                        string imgtexttemp = dt.Rows[i]["title"].ToString();
                        imgtext1 = imgtext1 + "|" + imgtexttemp;

                        string imglinktemp = basic.GetSiteUrl() + "site/s" + dt.Rows[i]["id"].ToString() + ".aspx?date=" + dt.Rows[i]["data"] + "";
                        imglink1 = imglink1 + "|" + imglinktemp;
                        string urlflag = Api.htmlpath.GetFullPath(dt.Rows[i]["id"].ToString(), "1", dt.Rows[i]["data"].ToString());
                        Words += "<li><a href=" + urlflag + "><img border='0' src='" + imgurltemp + "'></a></li>";

                    }

                    imgurl = imgurl1.Substring(1, imgurl1.Length - 1);
                    imgtext = imgtext1.Substring(1, imgtext1.Length - 1);
                    imglink = imglink1.Substring(1, imglink1.Length - 1);



                    string s = "<script>\r\n";
                    s += "var swf_width=" + width + ";\r\n";
                    s += "var swf_height=" + height + ";\r\n";
                    s += "var files='" + imgurl + "';\r\n";
                    s += "var links='" + imglink + "';\r\n";
                    s += "var texts='" + imgtext + "';\r\n";
                    s += "document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"'+ swf_width +'\" height=\"'+ swf_height +'\">');\r\n";
                    s += "document.write('<param name=\"movie\" value=\"" + basic.GetSiteMainWorkUrl() + "js/bcastr.swf\"><param name=\"quality\" value=\"high\">');\r\n";
                    s += "document.write('<param name=\"menu\" value=\"false\"><param name=wmode value=\"opaque\">');\r\n";
                    s += "document.write('<param name=\"FlashVars\" value=\"bcastr_file='+files+'&bcastr_link='+links+'&bcastr_title='+texts+'\">');\r\n";
                    s += "document.write('<embed src=\"" + basic.GetSiteMainWorkUrl() + "js/bcastr.swf\" wmode=\"opaque\" FlashVars=\"bcastr_file='+files+'&bcastr_link='+links+'&bcastr_title='+texts+'& menu=\"false\" quality=\"high\" width=\"'+ swf_width +'\" height=\"'+ swf_height +'\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />');\r\n";
                    s += "document.write('</object>')\r\n";
                    s += "</script>\r\n";
                    FTchina.Caching.FTchinaCache.AddCache("flash" + list, s);


                    return s;
                }
            }
        }
        /// <summary>
        /// 获取所有的flash的图片新闻
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static string GetFalshlist(int width, int height, int list)
        {
            return GetFalshlist(width, height, list, 0);
        }
        /// <summary>
        /// 获取所有左右滚动的图片新闻列表
        /// </summary>
        /// <param name="list">新闻数目</param>
        public static string GetLeftRightPics(int list)
        {
            return GetLeftRightPics(list, 0);
        }


        /// <summary>
        /// 获取左右滚动的图片新闻列表
        /// </summary>
        /// <param name="list">新闻数目</param>
        /// <param name="lanmuid">指定栏目id，0为所有</param>
        /// <returns></returns>
        public static string GetLeftRightPics(int list, int lanmuid)
        {
            if (FTchina.Caching.FTchinaCache.IsExits("GetLeftRightPics" + list + lanmuid))
            {
                return FTchina.Caching.FTchinaCache.GetCache("GetLeftRightPics" + list + lanmuid);
            }
            else
            {
                string flag = (lanmuid == 0) ? " 1=1 " : "lanmuid=" + lanmuid;

                string sql = "select top " + list + " * from content where  isdel=0 and ispass=1 and isimg=1 and " + flag + " ORDER BY id DESC ";
                DataTable dt = shujuku.GetDataTable(sql);
                string tempwords = "  <div id='demo' style='overflow: hidden; width: 100%; color: #ffffff'><table cellspacing='0' cellpadding='0' align='left' border='0' cellspace='0'><tbody><tr><td id='demo1' valign='top'>";

                tempwords += "<table width='" + (295 * dt.Rows.Count) + "' height='205' border='0' cellpadding='0' cellspacing='0'><tr>";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string urlflag = Api.htmlpath.GetFullPath(dt.Rows[i]["id"].ToString(), "1", dt.Rows[i]["data"].ToString());

                    tempwords += "<td width='295' background='" + basic.GetSiteMainWorkUrl() + "pic/pic_bg.jpg' align='center'><a href=" + urlflag + "><img src='" + dt.Rows[i]["img"] + "' border=0 width='270' height='170'></a></td>";
                }
                tempwords += "</tr></table>";


                tempwords += "</td><td id='demo2' valign='top'>&nbsp;</td></tr></tbody></table></div><script language='JavaScript' src='" + basic.GetSiteMainWorkUrl() + "js/scrollpic.js'></script>";
                FTchina.Caching.FTchinaCache.AddCache("GetLeftRightPics" + list + lanmuid, tempwords);
                return tempwords;
            }
        }

        #endregion


        #region 头条新闻
        /// <summary>
        /// 获取头条新闻【无图片】
        /// </summary>
        /// <param name="id">栏目id,0为所有</param>
        /// <param name="headtitlewords">标题字符数</param>
        /// <param name="jianjiewords">简介字符数</param>
        /// <returns></returns>
        public static string GetFirstHead(int id, int headtitlewords, int jianjiewords)
        {
            string flag = (id == 0) ? "  isdel=0 and ispass=1  " : "  isdel=0 and ispass=1 and lanmuid=" + id;
            DataTable dt = shujuku.GetDataTable("select top 1 * from content where " + flag + " order by id desc", "headnews");
            string tempwords = "<div id='firstHeadline'>";
            string urlflag = Api.htmlpath.GetFullPath(dt.Rows[0]["id"].ToString(), "1", dt.Rows[0]["data"].ToString());

            tempwords += "<center><a href=" + urlflag + "  target=\"_blank\" class=\"title\" title=\"" + basic.GetSubString(dt.Rows[0]["title"].ToString(), headtitlewords, "…") + "\" >" + basic.GetSubString(dt.Rows[0]["title"].ToString(), headtitlewords, "…") + "</a></center>" + basic.GetSubString(dt.Rows[0]["jianjie"].ToString(), jianjiewords, "…");
            tempwords += "</div>";
            return tempwords;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">栏目id,0为所有</param>
        /// <param name="headtitlewords">标题字符数</param>
        /// <param name="jianjiewords">简介字符数</param>
        /// <param name="list">显示list条新闻</param>
        /// <param name="timetype">
        /// 0:自动处理
        /// 1：yyyy年MM月dd日
        /// 2：MM月dd日
        /// 3：MM月dd日H时
        /// 4：yyyy年MM月dd日H时
        /// </param>
        /// <param name="words">标题显示words个字符</param>
        /// <param name="open">"0"或者不填写为"_self",其他的请行填写，如"_blank"</param>
        /// <param name="news">0代表不显示，1代表1天内的显示news图标，2代表2天内的显示news图标</param>
        /// <param name="lmname">0代表不显示栏目名称，1代表显示</param>
        /// <param name="hit">0代表不显示点击数，1代表显示</param>
        /// <param name="hot">0代表不按照点击量排序</param>
        /// <param name="dtj">0代表不显示【顶、荐、图】等字样，1代表显示</param>
        /// <param name="date">0代表不显示日期，1代表显示</param>
        /// <returns></returns>
        public static string GetFirstHeadFrame(int id, int headtitlewords, int jianjiewords, int list, int timetype, int words, string open, int news, int lmname, int hit, int hot, int dtj, int date)
        {
            string tempwords = "<div class=\"first\">";
            tempwords += GetFirstHead(id, headtitlewords, jianjiewords);
            tempwords += GetList(id, list, timetype, words, open, news, lmname, hit, hot, dtj, date);

            tempwords += "</div>";
            return tempwords;
        }
        #endregion

    }
}